package com.inifitness.tenant.core.system.controller.common;

import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inifitness.common.model.Option;
import com.inifitness.common.result.PageResult;
import com.inifitness.common.result.Result;
import com.inifitness.tenant.core.system.model.form.DictForm;
import com.inifitness.tenant.core.system.model.form.DictItemForm;
import com.inifitness.tenant.core.system.model.query.DictItemPageQuery;
import com.inifitness.tenant.core.system.model.query.DictPageQuery;
import com.inifitness.tenant.core.system.model.vo.DictItemPageVO;
import com.inifitness.tenant.core.system.model.vo.DictPageVO;
import com.inifitness.tenant.core.system.service.SysDictItemService;
import com.inifitness.tenant.core.system.service.SysDictService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "06.字典接口")
@RestController
@RequestMapping("/api/v1/dict")
@RequiredArgsConstructor
public class DictController {

  private final SysDictItemService dictService;

  private final SysDictService dictTypeService;

  @Operation(summary = "字典明细分页列表")
  @GetMapping("/item/page")
  public PageResult<DictItemPageVO> getDictItemPage(
      @ParameterObject DictItemPageQuery queryParams) {
    Page<DictItemPageVO> result = dictService.getDictItemPage(queryParams);
    return PageResult.success(result);
  }

  @Operation(summary = "字典明细数据表单数据")
  @GetMapping("/item/{id}/form")
  public Result<DictItemForm> getDictItemForm(
      @Parameter(description = "字典ID") @PathVariable(value = "id") Long id) {
    DictItemForm formData = dictService.getDictItemForm(id);
    return Result.success(formData);
  }

  @Operation(summary = "字典明细下拉列表")
  @GetMapping("/{dictCode}/options")
  public Result<List<Option>> listDictItemOptions(
      @Parameter(description = "字典类型编码") @PathVariable(value = "dictCode") String dictCode) {
    List<Option> list = dictService.listDictItemOptions(dictCode);
    return Result.success(list);
  }

  /*----------------------------------------------------*/
  @Operation(summary = "字典分页列表")
  @GetMapping("/page")
  public PageResult<DictPageVO> getDictPage(
      @ParameterObject DictPageQuery queryParams
  ) {
    Page<DictPageVO> result = dictTypeService.getDictPage(queryParams);
    return PageResult.success(result);
  }

  @Operation(summary = "字典表单数据")
  @GetMapping("/{id}/form")
  public Result<DictForm> getDictForm(
      @Parameter(description = "字典ID") @PathVariable(value = "id") Long id
  ) {
    DictForm dictForm = dictTypeService.getDictForm(id);
    return Result.success(dictForm);
  }
}
